package com.harmontronics.hanma.paas.paas.controller;

import java.util.Arrays;
import java.util.List;

import cn.hutool.core.bean.BeanUtil;
import com.harmontronics.hanma.paas.paas.common.ResponseEntity;
import com.harmontronics.hanma.paas.paas.common.PageBase;
import cn.hutool.core.convert.Convert;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.ApiParam;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import com.harmontronics.hanma.paas.paas.service.HmBaseModuleAppService;
import com.harmontronics.hanma.paas.paas.dto.iam.output.HmBaseModuleResult;
import com.harmontronics.hanma.paas.paas.dto.iam.input.HmBaseModuleCreateInput;
import com.harmontronics.hanma.paas.paas.dto.iam.input.HmBaseModuleUpdateInput;
import com.harmontronics.hanma.paas.paas.domain.iam.entity.HmBaseModule;
import com.harmontronics.hanma.paas.paas.utils.ParamUtils;

import javax.validation.Valid;


/**
 * 可见范围模块
 *
 * @author paas-generator
 * @email paas-generator@harmontronics.com
 * @date 2021-04-27 11:08:42
 */
@RestController
@RequestMapping("/api/hmbasemodule")
@Api(tags = {"可见范围模块"})
public class HmBaseModuleController {
    @Autowired
    private HmBaseModuleAppService hmBaseModuleappService;

    /**
     * 列表
     */
    @GetMapping("/list")
    @ApiOperation("列表")
    public ResponseEntity<List<HmBaseModuleResult>> list() {
        List<HmBaseModule> hmBaseModuleList = hmBaseModuleappService.getList();

        return ResponseEntity.ok(Convert.toList(HmBaseModuleResult.class, hmBaseModuleList));
    }

    /**
    * 分页列表
    */
    @GetMapping("/page/list")
    @ApiOperation("分页列表")
    public ResponseEntity<PageBase<HmBaseModuleResult>> list(@ApiParam(value = "页码", defaultValue = "1") @RequestParam(required = false, defaultValue = "1") int pageNo,
                                                             @ApiParam(value = "每页数据条数", defaultValue = "20") @RequestParam(required = false, defaultValue = "20") int pageSize) {
        PageBase<HmBaseModule> hmBaseModulePageBase = hmBaseModuleappService.getList(pageNo, pageSize);
        PageBase<HmBaseModuleResult> resultPageBase = PageBase.copy(hmBaseModulePageBase,HmBaseModuleResult.class);
        return ResponseEntity.ok(resultPageBase);
    }


    /**
     * 信息
     */
    @GetMapping("/info")
    @ApiOperation("详情")
    public ResponseEntity<HmBaseModuleResult> info(@RequestParam Long id) {
        HmBaseModule hmBaseModule =hmBaseModuleappService.findById(id);
            HmBaseModuleResult result = BeanUtil.toBean(hmBaseModule,HmBaseModuleResult.class);
        return ResponseEntity.ok(result);
    }

    /**
     * 保存
     */
    @PostMapping()
    @ApiOperation("保存")
    public ResponseEntity<String> save(@Valid @RequestBody HmBaseModuleCreateInput hmBaseModuleInput, BindingResult bindingResult) {
        ParamUtils.valid(bindingResult);
        HmBaseModule hmBaseModule =BeanUtil.toBean(hmBaseModuleInput,HmBaseModule. class);
            hmBaseModuleappService.save(hmBaseModule);

        return ResponseEntity.ok();
    }

    /**
     * 修改
     */
    @PutMapping
    @ApiOperation("修改")
    public ResponseEntity<String> update(@Valid @RequestBody HmBaseModuleUpdateInput hmBaseModuleInput, BindingResult bindingResult) {
        ParamUtils.valid(bindingResult);
        HmBaseModule hmBaseModule =BeanUtil.toBean(hmBaseModuleInput,HmBaseModule. class);
            hmBaseModuleappService.update(hmBaseModule);
        return ResponseEntity.ok();
    }

    /**
     * 删除
     */
    @DeleteMapping()
    @ApiOperation("删除")
    public ResponseEntity<String> delete(@RequestBody Long[] ids) {
            hmBaseModuleappService.remove(Arrays.asList(ids));

        return ResponseEntity.ok();
    }

}
